package com.atguigu.tingshu.payment.api;

import com.atguigu.tingshu.common.login.TsLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.payment.service.PaymentInfoService;
import com.atguigu.tingshu.payment.service.WxPayService;
import com.wechat.pay.java.service.payments.model.Transaction;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Tag(name = "微信支付接口")
@RestController
@RequestMapping("api/payment/wxPay")
@Slf4j
public class WxPayApiController {

    @Autowired
    private WxPayService wxPayService;

    @Autowired
    private PaymentInfoService paymentInfoService;


    /**
     * 微信支付回调
     * @return
     */
    @Operation(summary = "异步回调")
    @PostMapping("/notify")
    public Result wxNotify(HttpServletRequest request){
        System.out.println("异步回调!");
        //  调用服务层方法
        Transaction transaction = wxPayService.wxNotify(request);
        //  创建map集合;
        Map<String,Object> result = new HashMap<>();
        //  判断对象是否为空;
        if (null != transaction){
            //  支付成功;
            if (transaction.getTradeState().equals(Transaction.TradeStateEnum.SUCCESS)){
                //  修改交易记录与订单的状态;
                paymentInfoService.updatePaymentStatus(transaction);
                result.put("code","SUCCESS");
                result.put("message","成功");
                //  返回数据
                return Result.ok(result);
            }
        }
        //  默认返回失败
        result.put("code","FAIL");
        result.put("message","失败");
        //  返回数据;
        return Result.ok(result);
    }

    /**
     * 查看订单支付状态
     * @param orderNo
     * @return
     */
    @Operation(summary = "查看订单支付状态")
    @GetMapping("/queryPayStatus/{orderNo}")
    public Result queryPayStatus(@PathVariable String orderNo) {
        //   调用服务层方法-调用微信支付api接口;
        Transaction result = wxPayService.queryPayStatus(orderNo);
        if (null != result){
            //  支付成功;
            if (result.getTradeState().equals(Transaction.TradeStateEnum.SUCCESS)){
                //  修改交易记录与订单的状态;
                paymentInfoService.updatePaymentStatus(result);
                //  返回数据
                return Result.ok(true);
            }
        }
        //  返回数据
        return Result.ok(false);
    }

    /**
     * 微信支付
     *
     * @param orderNo
     * @return
     */
    @TsLogin
    @Operation(summary = "微信支付")
    @PostMapping("/createJsapi/{paymentType}/{orderNo}")
    public Result createJsapi(@PathVariable String orderNo,
                              @PathVariable String paymentType) {
        //  获取用户Id
        Long userId = AuthContextHolder.getUserId();
        //  调用服务层方法--回显支付地址,保存交易记录-(跟微信服务器进行对账);
        Map<String, Object> map = wxPayService.createJsapi(orderNo, paymentType, userId);
        //  返回数据
        return Result.ok(map);
    }
}
